package org.voidness.squaretilesframework.server;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.voidness.oje2d.Utils;

/**
 * A loader for an old map format Loads maps in the format: mapName width (in
 * tiles) height (int tiles) 0 0 pathToImage 0 1 pathToImage ...
 * 
 */
public class ServerSimpleLoader {
    /**
     * This method loads a map in an old format.
     * 
     * @param mFilename The filename of the map to load
     * @param mTileSize The tile size used in the map
     * @return The newly loaded map
     */
    public ServerMap load(String mFilename, int mTileSize) {
        try {
            // Open a buffer to the file contents
            BufferedReader dataFile =
                    new BufferedReader(new FileReader(mFilename));

            // Read the map name
            String mapName = dataFile.readLine();

            // Read the map's width and height (in tiles)
            int mapWidth = Integer.parseInt(dataFile.readLine().trim());
            int mapHeight = Integer.parseInt(dataFile.readLine().trim());

            // Start reading the tiles
            ServerMap map = new ServerMap(mapWidth, mapHeight);
            ServerTile tiles[][] = new ServerTile[mapWidth][mapHeight];
            tiles = initializeTiles(map, mapWidth, mapHeight);
            String line = null;
            String[] tileValues = null;
            while ((line = dataFile.readLine()) != null) {
                // Split the line using spaces
                tileValues = line.split(" ");

                // Get the tile's information
                int x = Integer.parseInt(tileValues[0].trim());
                int y = Integer.parseInt(tileValues[1].trim());
                // String fileName = tileValues[2].trim();
                boolean walkable =
                        Integer.parseInt(tileValues[3].trim()) == 0 ? true
                                : false;
                // String objectName = tileValues[4].trim();

                // Create the new tile
                tiles[x][y] = new ServerTile(map, walkable);
            }

            // Define the values
            map.setName(mapName);
            map.setFileName(mFilename.substring(mFilename.lastIndexOf("/"),
                    mFilename.length()));
            map.setTiles(tiles);

            // Return the new map
            return map;
        } catch (FileNotFoundException e) {
            Utils.dump("Map file not found: " + mFilename, Utils.ERROR);
            System.exit(1);
        } catch (IOException e) {
            Utils.dump("Error reading map file: " + mFilename, Utils.ERROR);
            System.exit(1);
        }

        // If we got here, some wicked stuff happened
        Utils.dump("Strange error reading map file: " + mFilename, Utils.ERROR);
        System.exit(1);
        return null;
    }

    /**
     * Initializes the temporary tiles
     * 
     * @param mMap The reference map to set in the tiles
     * @param mWidth The width of the map
     * @param mHeight The height of the map
     * @return The newly generated tile array
     */
    private ServerTile[][] initializeTiles(ServerMap mMap, int mWidth,
            int mHeight) {
        ServerTile[][] tiles = new ServerTile[mWidth][mHeight];
        for (int i = 0; i < mWidth; i++)
            for (int j = 0; j < mHeight; j++)
                tiles[i][j] = new ServerTile(mMap, true);
        return tiles;
    }
}